#ifndef WSListener_hpp
#define WSListener_hpp

#include <oatpp-websocket/WebSocket.hpp>
#include <oatpp-websocket/ConnectionHandler.hpp>
#include "ServerHandle.h"

/**
 * WebSocket listener listens on incoming WebSocket events.
 */
class WSListener : public oatpp::websocket::WebSocket::Listener {
private:
  static constexpr const char* TAG = "Server_WSListener";
  oatpp::data::stream::BufferOutputStream m_messageBuffer;
  ConnectHookPtr hookPtr;
public:
  WSListener(ConnectHookPtr _hookptr);
  void onPing(const WebSocket& socket, const oatpp::String& message) override;
  void onPong(const WebSocket& socket, const oatpp::String& message) override;
  void onClose(const WebSocket& socket, v_uint16 code, const oatpp::String& message) override;
  void readMessage(const WebSocket& socket, v_uint8 opcode, p_char8 data, oatpp::v_io_size size) override;
};

/**
 * Listener on new WebSocket connections.
 */
class WSInstanceListener : public oatpp::websocket::ConnectionHandler::SocketInstanceListener {
private:
  static constexpr const char* TAG = "Server_WSInstanceListener";
  ServerHandle* serverHandle;
public:
  static std::atomic<v_int32> SOCKETS;
  WSInstanceListener(ServerHandle* _serverHandle);
  void onAfterCreate(const oatpp::websocket::WebSocket& socket, const std::shared_ptr<const ParameterMap>& params) override;
  void onBeforeDestroy(const oatpp::websocket::WebSocket& socket) override;
};

#endif // WSListener_hpp
